<template>
  <breadcrumb :levelList="levelList" />
  <el-card class="card">
    <el-form v-if="state.showSearch==true" :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input v-model="queryForm.role_name" placeholder="角色名称" clearable />
      </el-form-item>
      <el-form-item>
      <el-button type="primary" @click="getDataList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
         <el-button type="primary" plain size="small" @click="addHandle(dataFormRef)" >新增</el-button>
      </el-col>
      <el-col :span="1.5">
         <el-button type="danger" plain size="small" :disabled="multiple" @click="handleDeleteBatch" >删除</el-button>
      </el-col>
    </el-row>
    <el-table :data="state.dataList" style="width: 100%" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="role_name" label="角色名称" width="180" />
      <el-table-column prop="role_key" label="权限字符" width="180" />
      <el-table-column label="状态" align="center" width="100">
       <template #default="scope">
        <el-button v-if="scope.row.status=='0'" size="small" type="primary" plain>正常</el-button>
        <el-button v-if="scope.row.status=='1'" size="small" type="warning" plain>禁用</el-button>
       </template>
    </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="editHandle(scope.row)">编辑</el-button>
          <el-button size="small"  type="danger"  @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination small background layout="prev, pager, next ,total, sizes" :total="state.total" v-model:page-size="state.limit" @change="pageChangeHandle" />
  </el-card>
  <el-dialog :title="state.dialogTitle" v-model="state.showDialog" width="600px" append-to-body>
    <el-form :model="state.dataForm" ref="dataFormRef" :rules="rules"  label-width="120px">
      <el-form-item label="角色名称" prop="role_name">
        <el-input placeholder="请输入角色名称" v-model="dataForm.role_name" />
      </el-form-item>
      <el-form-item label="权限字符" prop="role_key">
        <el-input placeholder="请输权限字符" v-model="dataForm.role_key" />
      </el-form-item>
      <el-form-item label="数据权限">
         <el-select v-model="dataForm.data_scope" >
            <el-option
               v-for="item in dataScopeOptions"
               :key="item.value"
               :label="item.label"
               :value="item.value"
            ></el-option>
         </el-select>
      </el-form-item>
      <el-form-item label="菜单权限">
        <el-tree
          class="tree-border"
          :data="menuOptions"
          show-checkbox
          ref="menuRef"
          node-key="id"
          :check-strictly="checkStrictly"
          empty-text="..."
          :props="{ label: 'name', children: 'children' }"
        ></el-tree>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio-button label="正常" value="0" />
          <el-radio-button label="禁用" value="1" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit(dataFormRef)">确 定</el-button>
          <el-button @click="cancelHandle">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, toRefs, onMounted } from 'vue'
import { CrudTool } from '@/utils/curd'
import { getRoleApiUrl, getRoleMenu } from '@/api/sys/role'
import { getMenu } from '@/api/sys/menu'
import breadcrumb from '@/views/common/breadcrumb.vue'

const checkStrictly = ref(false)
const dataFormRef = ref();
const levelList = ['首页','系统设置','角色管理']
const dataScopeOptions = [
  {label:'全部数据权限',value:'1'},
  {label:'自定数据权限',value:'2'},
  {label:'本部门数据权限',value:'3'},
  {label:'本部门及以下数据权限',value:'4'},
  {label:'仅个人数据',value:'5'},
];
const menuOptions = ref([])
const menuRef = ref(null)

const state = reactive({
  primaryKey:'role_id',
  queryForm: {
    role_name:'',
  },
  apiUrl: getRoleApiUrl(),
  total:0,
  dataForm:{
    role_id: 0,
    role_key: undefined,
    role_name: undefined,
    data_scope: '1',
    status: '0',
    remark: undefined,
    menuKeys:[]
  },
  titleText:['添加角色','修改角色'],
  rules:{
    role_name: [{ required: true, message: "角色名称不能为空", trigger: "blur" }],
    role_key:  [{ required: true, message: "权限符号不能为空", trigger: "blur" }],
  }

});

const { queryForm, dataForm, rules } = toRefs(state);
const { getDataList, addHandle, editHandle, cancelHandle, handleSubmit, handleDelete, handleDeleteBatch, addEventListener } = CrudTool(state)

addEventListener('beforOpenDialog', (data)=>{
  if( data.dataForm.role_id==0 ){
    menuRef.value?.setCheckedKeys([]);

  }else{
    menuRef.value?.setCheckedKeys([]);
    checkStrictly.value = true
    getRoleMenu(data.dataForm.role_id).then(res=>{
      menuRef.value?.setCheckedKeys(res.data);
      checkStrictly.value = false
    })

  }

  getMenu().then(res => {
    menuOptions.value = res.data;
  });
})

addEventListener('beforSubmit', (data)=>{
  data.dataForm.menuKeys = getMenuAllCheckedKeys();
})

/** 所有菜单节点数据 */
const getMenuAllCheckedKeys = () => {
  // 目前被选中的菜单节点
  let checkedKeys = menuRef.value?.getCheckedKeys();
  // 半选中的菜单节点
  let halfCheckedKeys = menuRef.value?.getHalfCheckedKeys();
  if (halfCheckedKeys) {
    checkedKeys?.unshift.apply(checkedKeys, halfCheckedKeys);
  }
  return checkedKeys;
}

getDataList()

</script>

<style scoped>
.card{
  min-height:600px;
}
.tree-border {
  margin-top: 5px;
  border: 1px solid #e5e6e7;
  background: #ffffff none;
  border-radius: 4px;
  width: 100%;
  padding:10px;
}
</style>